package com.jbh.wanandroidkotlin.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.util.*

class ActionBarView : FrameLayout {

    /*背景图*/
    private var actionBarBackground : Int ?= 0
    private var barColor : Int = 0
    /*title高度*/
    private var defaultHeight:Int = 0
    /**/
    private var toolBar : LinearLayout ?= null
    /*背景图片或颜色*/
    private var bgView : ImageView ?= null
    /*返回按钮的颜色*/
    private var backColor:Int = 0
    /*标题的颜色*/
    private var titleColor:Int = 0
    /*是否支持goBack*/
    private var isSupportBack : Boolean = false
    /*titleText*/
    private var titleText:String ?= null
    /*返回按钮*/
    private var backView:View?=null
    /*标题文字大小*/
    private var titleTextSize:Int= 16

    constructor(context: Context):this(context, null){
    }

    constructor(context: Context, attrs: AttributeSet?):this(context, attrs, 0){
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int):super(context, attrs, defStyleAttr){
        /**自定属性*/
        initAttr(attrs,defStyleAttr)
        /**实现沉嵌式状态栏*/
        getAppCompatActivity(context)?.let {
            initActionBar(it, when(barColor){
                0->TitleBarColor.white
                1->TitleBarColor.black
                else ->TitleBarColor.white
            })
        }

        /**支持添加背景*/
        addView(actionBarBackgroundView(),LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT))
        /**toolBarView*/
        toolBarView()
    }


    /**
     * 自定属性
     */
    private fun initAttr(attrs: AttributeSet?, defStyleAttr: Int) {
        val arrayAttr = context.obtainStyledAttributes(attrs, R.styleable.ActionBarView,0,defStyleAttr)
        defaultHeight = arrayAttr.getDimensionPixelOffset(R.styleable.ActionBarView_defaultHeight,dipToPx(context,50))
        actionBarBackground = arrayAttr.getResourceId(R.styleable.ActionBarView_actionBarBackground,0)
        backColor = arrayAttr.getInt(R.styleable.ActionBarView_backColor,0)
        titleColor = arrayAttr.getColor(R.styleable.ActionBarView_titleColor,Color.BLACK)
        isSupportBack = arrayAttr.getBoolean(R.styleable.ActionBarView_isSupportGoBack,false)
        titleText = arrayAttr.getString(R.styleable.ActionBarView_titleText)
        titleTextSize = arrayAttr.getInt(R.styleable.ActionBarView_titleTextSize,16)
        barColor = arrayAttr.getInt(R.styleable.ActionBarView_barColor,0)
        arrayAttr.recycle()
    }



    /**
     * bar
     */
    private fun toolBarView(): View? {
        toolBar = LinearLayout(context)
        toolBar?.let {
            it.gravity = Gravity.CENTER_VERTICAL
            addView(toolBar,LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, defaultHeight))
            val m = it.layoutParams as MarginLayoutParams
            m.topMargin = getStatusBarHeight(context)
        }
        defaultView()
        return toolBar
    }



    /**
     * 背景图
     */
    private fun actionBarBackgroundView(): View? {
        bgView = ImageView(context)
        bgView?.run {
            scaleType = ImageView.ScaleType.CENTER_CROP
            actionBarBackground?.let { setImageResource(it) }
        }
        return bgView
    }




    /**
     * 默认的标题栏
     */
    private fun defaultView(){
        /*返回按钮*/
        goBackView()
        /*title按钮*/
        titleView()
        /*右边按钮*/
        rightView()
    }



    /**
     * 右边按钮
     */
    private fun rightView() {
        toolBar?.run {
            val title = TextView(context)
            addView(title, LayoutParams(dipToPx(context,60),ViewGroup.LayoutParams.MATCH_PARENT))
        }
    }



    /**
     * 标题
     */

    private var titleContentView :LinearLayout?=null;
    private var tvTitleView:TextView ?=null
    private fun titleView(){
        toolBar?.run {
            titleContentView = LinearLayout(context)
                .apply {
                    orientation = LinearLayout.VERTICAL
                    gravity = Gravity.CENTER
                    tvTitleView = TextView(context)
                    addView(tvTitleView?.apply {
                                gravity = Gravity.CENTER
                                setTextColor(titleColor)
                                text = titleText
                                textSize = titleTextSize.toFloat()
                                setSingleLine() })
            }
            addView(titleContentView,LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1f))
        }
    }


    fun setTitle(title:String){
        tvTitleView?.let { it.text = title }
    }

    /**
     * 添加标题view
     */
    fun addTitleView(view: View,p:LinearLayout.LayoutParams){
        titleContentView?.let {
            it.removeAllViews()
            it.addView(view, p)
        }
    }


    fun addTitleView(view: View){
        titleContentView?.let {
            it.removeAllViews()
            it.addView(view)
        }
    }




    /**
     * 是否支持返回按钮
     */
    fun setSupportGoBack(b:Boolean){
        backView?.apply {
            visibility = if (b){
                VISIBLE
            }else{
                GONE
            }
        }
    }


    /**
     * 返回按钮
     *
     */
    private fun goBackView(){
        toolBar?.run {
            backView = ImageView(context).apply {
                if (isSupportBack){
                    this.visibility = View.VISIBLE
                }else{
                    this.visibility = View.INVISIBLE
                }

                setImageResource(when(backColor) {
                    0->R.mipmap.icon_black_back
                    1 -> R.mipmap.icon_white_back
                    else ->R.mipmap.icon_black_back })

                addView(this,LayoutParams(dipToPx(context,60),LayoutParams.WRAP_CONTENT))
                setOnClickListener {
                    getAppCompatActivity(context)?.finish()
                }
            }
        }
    }




    /***
     * 测量
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(measuredWidth,defaultHeight + getStatusBarHeight(context))
    }




    /**
     * 可以扩展任意布局
     */
    fun setAdapter(adapter: TitleBarAdapter){
        adapter?.let {
            toolBar?.let {
                it.removeAllViews()
                it.addView(adapter.layoutView(), LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT))
            }
        }
    }



    interface TitleBarAdapter{
        fun layoutView():View
    }



}